package model;

import java.util.ArrayList;
import java.util.EventListener;
import java.util.LinkedList;
import java.util.List;

import controller.Track;

public class RadioModel {
	
	private List<Track> queue = new LinkedList<Track>();
	private List<EventListener> listeners = new ArrayList<EventListener>();
	
	public void addTrack(Track track) {
		int index = queue.size();
		queue.add(index, track);
	}
	
	public Track getTrack() {
		if (queue.size() < 2) {
			notifyEmptyQueue();
		}
		Track track =  queue.get(0);
		queue.remove(0);
		return track;
	}
	
	public void addListener(EventListener listener) {
		listeners.add(listener);
	}
	
	private void notifyEmptyQueue() {
		for (EventListener listener : listeners) {
			if (listener instanceof RadioModelListener) {
				// Eventuell in einem Worker Thread abarbeiten
				((RadioModelListener) listener).emptyQueue();
			}
		}
	}
	
	public void printContent() {
		for (Track track : queue) {
			System.out.println(track.getId() + " Album: " + track.getAlbum() + "  Titel: " + track.getTitle());
		}
		System.out.println();	
	}
}
